package com.ruoyi.contract.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.contract.domain.PaymentCollection;
import com.ruoyi.contract.service.IPaymentCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 回款Controller
 *
 * @author ruoyi
 * @date 2024-06-15
 */
@RestController
@RequestMapping("/payment")
public class PaymentCollectionController extends BaseController
{
    @Autowired
    private IPaymentCollectionService paymentCollectionService;

    /**
     * 查询回款列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PaymentCollection paymentCollection)
    {
        startPage();
        List<PaymentCollection> list = paymentCollectionService.selectPaymentCollectionList(paymentCollection);
        return getDataTable(list);
    }

    /**
     * 导出回款列表
     */
    @Log(title = "回款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentCollection paymentCollection)
    {
        List<PaymentCollection> list = paymentCollectionService.selectPaymentCollectionList(paymentCollection);
        ExcelUtil<PaymentCollection> util = new ExcelUtil<PaymentCollection>(PaymentCollection.class);
        util.exportExcel(response, list, "回款数据");
    }

    /**
     * 获取回款详细信息
     */
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        return success(paymentCollectionService.selectPaymentCollectionByPaymentId(paymentId));
    }

    /**
     * 新增回款
     */
    @Log(title = "回款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PaymentCollection paymentCollection)
    {
        long i = paymentCollectionService.insertPaymentCollection(paymentCollection);
        if (i<=0){
            return new AjaxResult(208,"添加失败，最大可添加额度为："+(-i*0.01)+"元");
        }
        return success();
    }

    /**
     * 修改回款
     */
    @Log(title = "回款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PaymentCollection paymentCollection)
    {
        long i = paymentCollectionService.updatePaymentCollection(paymentCollection);
        if (i<0){
            return new AjaxResult(208,"修改失败，最大可添加额度为："+(-i*0.01)+"元");
        }
        return success();
    }

    /**
     * 删除回款
     */
    @Log(title = "回款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        return toAjax(paymentCollectionService.deletePaymentCollectionByPaymentIds(paymentIds));
    }
}

